package worktime.yft.demo.Service;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import worktime.yft.demo.Entity.Project;
import worktime.yft.demo.Mapper.ProjectMapper;

import java.util.List;

@Service
public class ProjectService {
    @Autowired
    private ProjectMapper projectMapper;
    public List<String> findAllProjectName(){
        return projectMapper.findAllProjectName();
    }
    public List<Integer> findAllProjectId(){
        return projectMapper.findAllProjectId();
    }
    public boolean insert(Project project){
        try{
            projectMapper.insert(project);
            return true;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
    }
    public boolean update(Project project){
        try{
            projectMapper.update(project);
            return true;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
    }

    public Project findProjectByName(String name){
        return projectMapper.findProjectByName(name);
    }
    public JSONArray findProjects(){
        JSONObject object;
        JSONArray array = new JSONArray();
        for(Project p:projectMapper.findProjects()){
            object = new JSONObject();
            object.put("name",p.getProjectName());
            object.put("note",p.getNote());
            object.put("isover",p.isOver());
            object.put("id",p.getProjectId());
            array.add(object);
        }
        return array;
    }
}
